package com.mojo.exception;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 捕获异常，并输出异常信息
 *
 * @author <a href="mailto:mojo_jj@163.com">Mojo</a>
 */
public class Exception1 {
    public static void main(String[] args) {
        //testException1();
        //testException2();
        //testArrayIndexOutOfBoundsException();
        int num = 50;
        num = ++num * 2;
        System.out.println(num);
        List<Integer> objects = new ArrayList<>();
        objects.add(1);
        objects.add(3);
        objects.add(8);
        objects.add(4);
        objects.add(8 );
        objects.add(7 );
        objects.add(99 );
        objects.add(9 );
        List<Integer> integers = Arrays.asList(1, 3, 8, 4, 2, 8, 4, 99, 7);
        Set<Integer> collect = objects.stream().collect(Collectors.toSet());
        for (Integer integer : collect) {
            System.out.print(integer+" ");
        }
    }

    private static void testException1() {
        try {
            throw new RuntimeException("运行时异常");
        } catch (RuntimeException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }

    public static void testException2() {
        Integer i = null;
        try {
            i.intValue();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }

    public static void testArrayIndexOutOfBoundsException() {
        int[] intArr = {1, 2};
        try {
            System.out.println(intArr[2]);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }

    public static void testDefineException() {
        try {
            throw new DefineException("自定义异常信息");
        } catch (DefineException e) {
            e.printStackTrace();

        }
    }
}
